package com.mtor.systeminfo;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.widget.TextView;

public class SystemInfo extends Activity {
	private TextView totalInternalMemoryText, availableInternalMemoryText, internalPathText,
			totalExternalMemoryText, availableExernalMemoryText, externalPathText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.system_info);
		
		double totalInternalMemory = getTotlaInternalMemorySize();
		totalInternalMemoryText = (TextView)findViewById(R.id.total_internal_memory);
		totalInternalMemoryText.setText(String.format("%.3fMB", totalInternalMemory/1024/1024));
		
		double availableinternalMemory = getAvailableInternalMemorySize();
		availableInternalMemoryText = (TextView)findViewById(R.id.available_internal_memory);
		availableInternalMemoryText.setText(String.format("%.3fMB", availableinternalMemory/1024/1024));
		
		internalPathText = (TextView)findViewById(R.id.internal_path);
		internalPathText.setText(Environment.getDataDirectory().toString());
		
		
		double totalExternalMemory = getTotlaExternalMemorySize();
		totalExternalMemoryText = (TextView)findViewById(R.id.total_external_memory);
		if(totalExternalMemory == -1)
			totalExternalMemoryText.setText("0.0GB");
		else
			totalExternalMemoryText.setText(String.format("%.3fGB", totalExternalMemory/1024/1024/1024));
		
		double availableExernalMemory = getAvailableExternalMemorySize();
		availableExernalMemoryText = (TextView)findViewById(R.id.available_external_memory);
		if(availableExernalMemory == -1)
			availableExernalMemoryText.setText("0.0GB");
		else
			availableExernalMemoryText.setText(String.format("%.3fGB", availableExernalMemory/1024/1024/1024));
		
		externalPathText = (TextView)findViewById(R.id.external_path);
		externalPathText.setText(Environment.getExternalStorageDirectory().toString());
		
	}
	
	public double getTotlaInternalMemorySize(){
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		
		return totalBlocks * blockSize;
	}
	
	public double getAvailableInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		
		return availableBlocks * blockSize;
	}
	
	public boolean isExistExternalMemory(){
		return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
	}
	
	public double getAvailableExternalMemorySize(){
		if(isExistExternalMemory()){
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			
			return availableBlocks * blockSize;
		}
		else{
			return -1;
		}
	}
	
	
	public double getTotlaExternalMemorySize(){
		if(isExistExternalMemory()){
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long totalBlocks = stat.getBlockCount();
			
			return totalBlocks * blockSize;
		}
		else{
			return -1;
		}
		
	}
	

}
